package com.wwwarehouse.common.activity.map;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.wwwarehouse.common.R;
import com.wwwarehouse.common.adapter.map.SearchAddressListAdapter;
import com.wwwarehouse.common.bean.map.NearAddressBean;
import com.wwwarehouse.common.eventbus_event.NearBeanEvent;
import com.wwwarehouse.common.eventbus_event.SearchResultEvent;
import com.wwwarehouse.common.eventbus_event.StringEvent;

import java.util.List;

import de.greenrobot.event.EventBus;

/**
 * Description:
 * Copyright  : Copyright (c) 2017
 * Author     : yfwang
 * Date       : 2017/9/23 10:16
 */
public class SearchMapActivity extends Activity implements SearchAddressListAdapter.OnItemClickListener {
    private EditText mSearch;
    private RecyclerView mRecyclerView;
    private SearchAddressListAdapter addressListAdapter;
    private TextView mNoResult;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search_map);
        mSearch = (EditText) findViewById(R.id.et_serach);
        mRecyclerView = (RecyclerView) findViewById(R.id.rv_search);
        mNoResult = (TextView) findViewById(R.id.tv_no_result);

        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        //设置布局管理器
        mRecyclerView.setLayoutManager(layoutManager);
        layoutManager.setOrientation(OrientationHelper.VERTICAL);

        EventBus.getDefault().register(this);

        final Intent intent = getIntent();

        if (intent == null) {
            return;
        }

        if (addressListAdapter == null) {
            addressListAdapter = new SearchAddressListAdapter(SearchMapActivity.this);
        }
        addressListAdapter.setOnItemclickListener(this);

        mSearch.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                EventBus.getDefault().post(new StringEvent(s.toString().trim()));

            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

    }

    public void onEventMainThread(Object event) {
        if (event instanceof SearchResultEvent) {
            List<NearAddressBean> address = ((SearchResultEvent) event).getAddress();
            if (address != null) {
                mNoResult.setVisibility(View.GONE);
                mRecyclerView.setVisibility(View.VISIBLE);
                addressListAdapter.setData(address);
                mRecyclerView.setAdapter(addressListAdapter);
            } else {
                mNoResult.setVisibility(View.VISIBLE);
                mRecyclerView.setVisibility(View.GONE);
            }
        }

    }

    @Override
    public void itemClick(NearAddressBean nearAddressBean, int position) {
        EventBus.getDefault().post(new NearBeanEvent(nearAddressBean));
        SearchMapActivity.this.finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
